using System.Collections.Generic;
using System.Xml.Linq;
using WB.Mvc.Forms.Widgets;

namespace WB.Mvc.Forms.Widgets
{
	/// <summary>
	/// A text input widget.
	/// <seealso cref="Widget"/>
	/// </summary>
	public class TextInput : Widget
	{
		private const string idAttributeName = "id";

		/// <summary>
		/// Creates an instance of <see cref="TextInput"/>.
		/// </summary>
		public TextInput(string name)
			: this(name, new Dictionary<string, object>())
		{
		}

		/// <summary>
		/// Creates an instance of <see cref="TextInput"/>.
		/// </summary>
		/// <param name="maxLength">Maximum length of the input that the text input widget accepts.</param>
		public TextInput(string name, uint maxLength)
			: this(name, AttributeDictionaryBuilder.New().Add("maxLength", maxLength).ToDictionary())
		{
		}


		/// <summary>
		/// Creates an instance of <see cref="TextInput"/>.
		/// </summary>
		public TextInput(string name, IDictionary<string, object> attributes)
			: base(name, attributes)
		{
			if (!Attributes.ContainsKey(idAttributeName))
				Attributes.Add(idAttributeName, name);
			if (!Attributes.ContainsKey("value"))
				Attributes.Add("value", string.Empty);
		}

		/// <summary>
		/// Maximum length of the input that the text input widget accepts.
		/// </summary>
		public virtual uint MaxLength
		{
			get { return (uint) (Attributes["maxLength"] ?? 0); }
		}

		public override string ToHtml()
		{
			var ele = new XElement("input", new XAttribute("type", "text"));

			foreach (var attribute in Attributes)
			{
				ele.Add(new XAttribute(attribute.Key, attribute.Value));
			}

			return ele.ToString();
		}
	}

	/// <summary>
	/// An item in a list.
	/// </summary>
	public interface IListItem
	{
		string Text { get; set; }

		string Value { get; set; }
	}

	public class ListItem: IListItem
	{
		public string Text { get; set; }

		public string Value { get; set; }
	}
}